package cn.gqr.controller;

import cn.gqr.entity.Order;
import cn.gqr.entity.OrderLine;
import cn.gqr.entity.User;
import cn.gqr.service.OrderService;
import cn.gqr.tool.GetUser;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//订单
@RestController
@RequestMapping("/api")
public class OrderController {

    @Autowired
    OrderService orderService;

//后端

    //查询订单头信息(含用户信息)
    @RequestMapping("/adm/ord/findOrdInfo/{start}/{num}")
    public  Map<String, Object> findOrdInfo(@PathVariable("start")int start,OrderLine orderLine,Integer ispay,
                                            @PathVariable("num")int num, String tstart , String tend ){
        return orderService.findOrdInfo(orderLine,ispay,tstart,tend,start,num);
    }

    // 根据 订单 id, 查找该订单对应的订单明细信息(含书本)
    @RequestMapping("/adm/ord/findItemInfoByOrdid/{ordid}")
    public  Map<String, Object> findItemInfoByOrdid(@PathVariable("ordid")int ordid){
        return  RespJson.pack(200,"",orderService.findItemInfoByOrdid(ordid));
    }

    //添加发货地址
    @RequestMapping("/adm/ord/addShip")
    public  Map<String, Object> addShip(@RequestBody Order ord){
        System.out.println(ord);
        int i = orderService.addShip(ord);
        return RespJson.pack(200,"添加快递成功...",i);
    }


//前端

    //查找该登录用户的下某一个订单号
    @RequestMapping("/fore/ord/findInfoByOrdid/{ordid}")
    public Map<String, Object> findInfoByOrdid(@PathVariable("ordid") int ordid){
        User user= (User)GetUser.getUser();
        return RespJson.pack(200,"",orderService.findInfoByOrdid(user.getId(),ordid));
    }

    //更新发货地址及电话
    @RequestMapping("/fore/ord/addAddr")
    public Map<String, Object> addAddr (@RequestBody Order ord){
        return RespJson.pack(200,"添加地址成功",orderService.addAddr(ord));
    }

    // 更新付款标识
    @RequestMapping("/fore/ord/addPay")
    public Map<String, Object> addPay(@RequestBody Order ord){
        Order order = null;
        if(orderService.addPay(ord)!=0){
            order = orderService.findByIdOrder(ord.getOrdid());
        }
        return RespJson.pack(200,order!=null?"付款成功....":"付款失败",order);
    }

    //查找该登录用户的所有订单头信息
    @RequestMapping("/fore/ord/findOrdLines")
    public Map<String, Object> findOrdLines(Integer ispay,String tstart,String tend){
        User user= (User) GetUser.getUser();
        if(user.getId()==0){
            return RespJson.pack(401,"Token过期，请重新登陆",null);
        }
        List<OrderLine> ordLines = orderService.findOrdLines(user.getId(), ispay, tstart, tend);
        if(ordLines.isEmpty()){
            return  RespJson.pack(500,"查询数据有误",null);
        }
        return RespJson.pack(200,"",ordLines);
    }

    // 查找该登录用户的所有订单头信息 分页
    //start起始页 num数量 ispay是否付款 tstart起始时间 tend截止时间
    @RequestMapping("/fore/ord/findOrdLineByPage/{start}/{num}")
    public Map<String, Object> findOrdLineByPage(@PathVariable("start") int start,@PathVariable("num") int num,
                                    Integer ispay,String tstart,String tend){
        //获取用户
        User user= (User)GetUser.getUser();
        //判断有无用户
        if(user.getId()==0){
            return RespJson.pack(401,"Token过期，请重新登陆",null);
        }
        return orderService.findOrdLineByPage(user.getId(), ispay, tstart, tend, start, num);
    }

    // 根据 订单 id, 查找该订单对应的订单明细信息(含书本)
    @RequestMapping("/fore/ord/findItemInfoByOrdid/{ordid}")
    public  Map<String, Object> foreFindItemInfoByOrdid(@PathVariable("ordid")int ordid){
        return  RespJson.pack(200,"",orderService.findItemInfoByOrdid(ordid));
    }

}
